package com.xing.radar.main;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.helper.StringUtil;

import com.xing.radar.bean.WebSite;

public class PostFactory {

	private static HttpPost httppost;
	private static WebSite webSite;
	static int i = 0;

	public static String setHttpPost(String post, boolean print) {
		if (httppost == null) {
			httppost = new HttpPost();
		}
		try {
			URI uri = new URI(post);
			httppost.setURI(uri);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (webSite != null && !StringUtil.isBlank(webSite.getCookies())) {
			httppost.setHeader("Cookie", webSite.getCookies());
		}
		HttpClient httpclient = new DefaultHttpClient();
		HttpHost proxy = new HttpHost("127.0.0.1", 8087);
	    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		String html = "";
		try {
			if (httppost.isAborted()) {
				httppost.reset();
			}
			HttpResponse response = httpclient.execute(httppost);
			System.out.println(response.getStatusLine());
			html = EntityUtils.toString(response.getEntity(), webSite.getCharSet());
			if (print) {
				System.out.println(html);
			}
			httppost.abort();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return html;
	}

	public static String getCookies() throws Exception {
		DefaultHttpClient client = new DefaultHttpClient();
		HttpHost proxy = new HttpHost("127.0.0.1", 8087);
		client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		HttpGet request = new HttpGet(webSite.getLoginUrl() + "?" + webSite.getPostData());
		client.execute(request);
		java.util.List<Cookie> cookies = client.getCookieStore().getCookies();
		String tmpcookies = assemblyCookie(cookies);
		return tmpcookies;
	}

	public static String postCookies() throws Exception {
		DefaultHttpClient client = new DefaultHttpClient();
		HttpHost proxy = new HttpHost("127.0.0.1", 8087);
		client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		HttpPost httppost = new HttpPost();
		URI uri = new URI(webSite.getLoginUrl());
		httppost.setURI(uri);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		String t[] = webSite.getPostData().split("&");
		for (int i = 0; i < t.length; i++) {
			String s[] = t[i].split("=");
			System.out.println(s[0] + "---" + s[1]);
			nvps.add(new BasicNameValuePair(s[0], s[1]));
		}
		httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
		client.execute(httppost);
		java.util.List<Cookie> cookies = client.getCookieStore().getCookies();
		String tmpcookies = assemblyCookie(cookies);
		return tmpcookies;
	}

	public static String assemblyCookie(List<Cookie> cookies) {
		StringBuffer sbu = new StringBuffer();
		for (Cookie cookie : cookies) {
			sbu.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
		}
		if (sbu.length() > 0)
			sbu.deleteCharAt(sbu.length() - 1);
		return sbu.toString();
	}

	public static WebSite getWebSite() {
		return webSite;
	}

	public static void setWebSite(WebSite webSite) {
		PostFactory.webSite = webSite;
	}

}
